home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / ColorView.BackModule / ColorView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.1 KB  |  194 lines

  1.  
  2. #import "ColorView.h"
  3. #import <math.h>
  4. #import <libc.h>
  5. #import <dpsclient/wraps.h>
  6. #import <appkit/color.h>
  7. #import <appkit/Button.h>
  8. #import <appkit/Application.h>
  9. #import <appkit/NXColorWell.h>
  10.  
  11. @implementation ColorView
  12.  
  13. - initFrame:(NXRect *)frameRect
  14. {
  15.     [super initFrame:frameRect];
  16.     srandom(getpid());
  17.     count = 0;
  18.     colChange = 1;
  19.     flip = 0;
  20.     speed = 68;
  21.     granularity = 150;
  22.     maxBright = 1.0;
  23.     minBright = 0.0;
  24.     [NXApp loadNibSection:"ColorPrefs.nib" owner:self];
  25.     winNum=[[self window] windowNum];
  26.  
  27.     // initial color is a bright blue.
  28.     staticColor = NXConvertRGBToColor(0.0, 0.0, 1.0);
  29.     currentColor = NXConvertRGBToColor(0.0, 0.0, 1.0);
  30.     nextColor = NXConvertRGBToColor(0.0, 0.0, 1.0);
  31.     [colorWell setColor:staticColor];
  32.     return self;
  33. }
  34.  
  35. - sizeTo:(NXCoord)width :(NXCoord)height
  36. {
  37.     [super sizeTo:width :height];
  38.     mywidth = width;
  39.     myheight = height;
  40.     return self;
  41. }
  42.  
  43. NXColor fade(currentColor, nextColor, tick, granularity)
  44. NXColor currentColor, nextColor;
  45. int tick, granularity;
  46. {
  47.     float fadeVal = tick * 1.0 / granularity;
  48.     float r, g, b;
  49.     NXColor tempColor;
  50.     
  51.     float r1 = NXRedComponent(currentColor);
  52.     float r2 = NXRedComponent(nextColor);
  53.     float g1 = NXGreenComponent(currentColor);
  54.     float g2 = NXGreenComponent(nextColor);
  55.     float b1 = NXBlueComponent(currentColor);
  56.     float b2 = NXBlueComponent(nextColor);
  57.  
  58.     r = r1 + fadeVal * (r2 - r1);
  59.         g = g1 + fadeVal * (g2 - g1);
  60.     b = b1 + fadeVal * (b2 - b1);
  61.     tempColor = NXConvertRGBToColor(r, g, b);
  62.     //fprintf(stderr, "Colors: %f %f %f\n", r, g, b);
  63.     return tempColor;
  64. }
  65.  
  66. - oneStep
  67. {
  68.     float brMult = maxBright - minBright;
  69.     if (brMult < 0.0) brMult = 0.0;
  70.     brMult = brMult/MAXINTF;
  71.     
  72.     tick = ++tick % speed; // allows us to change fade speed
  73.     if (!colChange) showColor = staticColor; // no fades
  74.     else if (!tick) { // only fade colors when right tick
  75.     if (colRandom) { // fade to new (random) color
  76.         count = ++count % granularity;
  77.         if (!count) {
  78.             currentColor = nextColor;
  79.             nextColor = NXConvertRGBToColor(random() * brMult +
  80.               minBright, random() * brMult + minBright, random()
  81.               * brMult + minBright);
  82.         }
  83.         showColor = fade(currentColor, nextColor, count, granularity);
  84.     } else { // fade color in and out
  85.         count = ++count % granularity;
  86.         if (!count) { if (!flip) {
  87.             currentColor =
  88.               NXChangeBrightnessComponent(staticColor, minBright);
  89.             nextColor =
  90.               NXChangeBrightnessComponent(staticColor, maxBright);
  91.             flip = 1;
  92.         } else {
  93.             nextColor =
  94.               NXChangeBrightnessComponent(staticColor, minBright);
  95.             currentColor =
  96.               NXChangeBrightnessComponent(staticColor, maxBright);
  97.             flip = 0;
  98.         } }
  99.         showColor = fade(currentColor, nextColor, count, granularity);
  100.     } }
  101.     if (!tick || !colChange) [self update]; // only update when needed
  102.     //fprintf(stderr,"tick=%d, count=%d\n",tick,count);
  103.     return self;
  104. }
  105.  
  106. - drawSelf:(const NXRect *)rects :(int)rectCount
  107. {
  108.     if (!rects || !rectCount) return self;
  109.     
  110.     NXSetColor(showColor);
  111.     NXRectFill(rects);
  112.     
  113.     PScurrentmouse(winNum, &curX, &curY);
  114.     /* Test the cursor */
  115.     if (mywidth - curX + myheight - curY <= 10.0)
  116.         [self show]; // get Pref panel if in upper rt. corner
  117.  
  118.  
  119.     return self;
  120. }
  121.  
  122. - (BOOL)isBoringScreenSaver
  123. {    // I hope I set this right...
  124.     return NO;
  125. }
  126.  
  127. - (BOOL) useBufferedWindow
  128. { // unbuffered, so a call to update will always call drawself
  129.   // to fill the window with color...
  130.     return NO;
  131. }
  132.  
  133.  
  134. - show
  135. {
  136.     [myPrefWindow orderFront:nil];
  137.     return self;
  138. }
  139.  
  140. - newColor:sender
  141. {
  142.     staticColor = [sender color];
  143.     return self;
  144. }
  145.  
  146. - newMaxBright:sender
  147. {
  148.     maxBright = [sender floatValue];
  149.     if (minBright > maxBright) {
  150.         minBright = maxBright;
  151.         [minSlider setFloatValue:minBright];
  152.     }
  153.     return self;
  154. }
  155.  
  156. - newMinBright:sender
  157. {
  158.     minBright = [sender floatValue];
  159.     if (minBright > maxBright) {
  160.         maxBright = minBright;
  161.         [maxSlider setFloatValue:maxBright];
  162.     }
  163.     return self;
  164. }
  165.  
  166. - newGranularity:sender
  167. {
  168.     granularity = [sender intValue];
  169.     return self;
  170. }
  171.  
  172. - newSpeed:sender
  173. {
  174.     speed = [sender intValue];
  175.     return self;
  176. }
  177.  
  178. - colorsChange:sender
  179. {
  180.     colChange = [sender state];
  181.     flip = 0; count = 0; tick = 0;
  182.     return self;
  183. }
  184.  
  185. - colorsRandom:sender
  186. {
  187.     colRandom = [sender state];
  188.     count = 0; tick = 0;
  189.     return self;
  190. }
  191.  
  192.  
  193. @end
  194.